package src.string;

public class no925 {
    //长按键入
    public static void main(String[] args) {
        String s = "saeed";
        String t = "ssaaedd";
        System.out.println(isLongPressedName(s, t));
    }

    public static boolean isLongPressedName(String name, String typed) {
        int m = name.length(), n = typed.length();
        if (name.charAt(0) != typed.charAt(0)) return false;
        int i = 0, j = 0;  //i 遍历name，j 遍历typed
        while (i < m && j < n) {
            if (name.charAt(i) == typed.charAt(j)) {
                i++;
                j++;
            } else {
                //不相等时先让 j 后移，找到下一个字母再对比
                //判断边界为n-1,若为n会越界,例如name:"kikcxmvzi" typed:"kiikcxxmmvvzzz"出错
                while (j < n - 1 && typed.charAt(j) == typed.charAt(j - 1)) {
                    j++;
                }
                if (name.charAt(i) == typed.charAt(j)) {
                    i++;
                    j++;
                } else return false;
            }
        }

        //name没有匹配完
        if (i < m) return false;
        //type没有匹配完
        while (j < n) {
            if (typed.charAt(j) == typed.charAt(j - 1)) {
                j++;
            } else return false;
        }

        return true;
    }
}
